package dao;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import bean.*;
import java.sql.*;
import java.util.stream.StreamSupport;


public class Userdao {
    //add users
    public boolean addUser(User user) {
        DBbean db = new DBbean();
        db.getConnection();

        String sql = "insert into user (user_name,user_password, email, kind) values (?,?,?,?)";
        ArrayList params = new ArrayList();

        params.add(user.getUser_name());
        params.add(user.getUser_password());
        params.add(user.getEmail());
        params.add(user.getKind());

        try {
            db.executeUpdate(sql, params);
        } catch (Exception e) {
//            e.printStackTrace();
            db.close();
            return false;
        }
        db.close();
        return true;
    }


    public boolean login(String user_name, String user_password, String kind) {
        DBbean db = new DBbean();
        db.getConnection();
        String sql = "select user_name, user_password, kind from user where user_name = \"" + user_name + "\"";
        ResultSet rs = null;
        try {
            rs = db.executeQuery(sql, null);
            if (rs.next()) {
//                System.out.println(rs.getString(1) + "++++++++++++++++++++++++++++");
//                System.out.println(rs.getString(3) + "++++++++++++++++++++++++++++");
                if (rs.getString(1).equals(user_name) && rs.getString(3).equals(kind) && rs.getString(2).equals(user_password)) {
                    db.close();
                    return true;
                }
            } else {
                db.close();
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        db.close();
        return false;
    }


    public User getUser(String username) {
        String sql = "select * from user where user_name = \"" + username + "\"";
        DBbean db = new DBbean();
        db.getConnection();
        ResultSet rs = null;
        User ret = new User();
        try {
            rs = db.executeQuery(sql, null);
            rs.next();
            ret.setEmail(rs.getString("email"));
            ret.setUser_name(rs.getString("user_name"));
            ret.setKind(rs.getString("kind"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        db.close();
        return ret;
    }


    // 修改用户信息
    public boolean updateUser(User user) {
        DBbean db = new DBbean();
        db.getConnection();
        String sql = "update user set user_password = ?,user_email = ? where user_name =\"" + user.getUser_name() + "\"";
        ArrayList params = new ArrayList();
        params.add(user.getUser_password());
        params.add(user.getEmail());
        try {
            int i = db.executeUpdate(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
            db.close();

            return false;
        }
        db.close();
        return true;
    }
}


